package ck.mmd.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import ck.mmd.bean.News;
import ck.mmd.bean.Remark;
import ck.mmd.dao.NewsDao;
import ck.mmd.dao.RemarkDao;
import ck.mmd.dao.UserDao;
import ck.mmd.util.JsonUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class NewsAction  extends ActionSupport  implements SessionAware,ModelDriven<News>{

	private static final long serialVersionUID = 1L;
	private News news;
	HttpServletRequest request = ServletActionContext.getRequest();
	HttpServletResponse response = ServletActionContext.getResponse();
	NewsDao newsdao = new NewsDao();
	UserDao userdao = new UserDao();
	private RemarkDao remarkdao = new RemarkDao();
	
	/**
	 * 新闻的首页
	 * return 返回list
	 */
	public String execute(){
		ActionContext context = ActionContext.getContext();
		//获取所有的新闻
		List<News> newslist = new ArrayList<News>();
		String type = "10001";  //类别为新闻
		newslist = newsdao.findAllNewsByType(type);
		if(newslist.size() <= 0){
			context.put("isnull", 0);
			//context.put("list", newslist);
			context.put("msg", "没有发布了的新闻...");
		}else{
			Iterator<News> it =  newslist.iterator();
			List<News> list = new ArrayList<News>();
			int size = 0;
			while(it.hasNext()){
				News news = it.next();
				news.setUser(userdao.findUserById(news.getUp_id()));
				list.add(news);
				size ++;
			}
			context.put("size", size);
			context.put("isnull", 1);
			context.put("list", list);
		}
		return "success";
	}
	
	/**
	 * 获取文章详情
	 * @return 返回详情对象
	 */
	public String detail(){
		ActionContext context = ActionContext.getContext();
		String id = request.getParameter("key");
		News news = new News();
		news = newsdao.findNewsById(id);
		if(news == null){
			context.put("isnull", 0);
			context.put("msg", "SORRY...文章已经被搬运走了...");
		}else{
			context.put("isnull", 1);
			news.setUser(userdao.findUserById(news.getUp_id()));
			
			//查询评论
			List<Remark> remarklist = new ArrayList<Remark>();
			remarklist = remarkdao.findRemarksByUId(news.getId());
			if(remarklist.size() <= 0){
				context.put("size","0");
			}else{
				Iterator<Remark> it =  remarklist.iterator();
				List<Remark> remarklistdemo = new ArrayList<Remark>();
				while(it.hasNext()){
					Remark remark = it.next();
					remark.setUser(userdao.findUserById(remark.getUser_id()));
					remarklistdemo.add(remark);
				}
				context.put("size","1");
				context.put("remarklist",remarklistdemo);
			}
			context.put("item", news);
		}
		return "detail";
	}
	
	/**
	 * 查找最新的size个新闻
	 * @return
	 * @throws IOException 
	 */
	public String top_img() throws IOException{
		//ActionContext context = ActionContext.getContext();
		int size = Integer.parseInt(request.getParameter("size"));
		//String type = "10001";  //类别为新闻
		List<News> list = newsdao.findHotNewsBySort(size);
		String liststr = JsonUtil.list2json(list);
		System.out.println(liststr);
		response.getWriter().print(liststr);
		return null;
	}
	
	/**
	 * 热门阅读
	 * @return
	 * @throws IOException
	 */
	public String remenyuedu() throws IOException{
		int size = Integer.parseInt(request.getParameter("size"));
		List<News> list = newsdao.findHotNewsBySort(size);
		String liststr = JsonUtil.list2json(list);
		response.getWriter().print(liststr);
		return null;
	}
	
	/**
	 * 最新文章
	 * @return
	 * @throws IOException
	 */
	public String zuixin() throws IOException{
		int size = Integer.parseInt(request.getParameter("size"));
		List<News> list = newsdao.findNewsBySort(size);
		String liststr = JsonUtil.list2json(list);
		response.getWriter().print(liststr);
		return null;
	}
	
	public void setSession(Map<String, Object> arg0) {
		
	}

	public News getModel() {
		if(this.news == null){
			return this.news = new News();
		}
		return this.news;
	}
}
